package data_report

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type AgentReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 代理报表
func NewAgentReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AgentReportLogic {
	return &AgentReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AgentReportLogic) AgentReport(req *types.AgentReportRequest) (resp *types.AgentReportResponse, err error) {
	resp = &types.AgentReportResponse{
		Data:     make([]types.AgentReportItem, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	reqq := &rpc.AgentAnalysisReportRequest{
		UserId:    req.UserId,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		Sort:      req.Sort,
		AgentType: req.AgentType,
		Level:     req.Level,
		Page:      req.Page,
		PageSize:  req.PageSize,
	}

	analysis, err := l.svcCtx.Analysis.AgentAnalysisReport(l.ctx, reqq)
	if err != nil {
		return resp, err
	}
	for _, v := range analysis.List {
		tt := types.AgentReportItem{}
		copier.Copy(&tt, v)
		resp.Data = append(resp.Data, tt)
	}
	resp.Total = analysis.Total
	return
}
